﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MenesDroid;
using Microsoft.Xna.Framework.Content;
using XNA_Game_Engine.Sounds;
using XNA_Game_Engine.FileManagment;

namespace XNA_Game_Engine.Canvas
{
    public class MainMenuCanvas : Canvas
    {
        private Texture2D logo;        
        private StateButton muteButton, fullScreenButton, tipsButton;

        public MainMenuCanvas( Game game )
        {
            Width = game.Window.ClientBounds.Width;
            Height = game.Window.ClientBounds.Height;
            Buttons = new Botoes();
            Rand = new Random();
        }

        //
        public override void LoadContent(ContentManager content)
        {
            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Buttons\play_button"),
                                    content.Load<Texture2D>(@"Buttons\play_button"),
                                    new Vector2( Width / 2 - 255, 300), new Vector2(255, 75),
                                    ActionsHandler.StartButtonEvent));

            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Buttons\exit_button"),
                                   content.Load<Texture2D>(@"Buttons\exit_button"),
                                   new Vector2(Width / 2 - 255, 400), new Vector2(255, 75),
                                   ActionsHandler.ExitButtonEvent));


            

            logo = content.Load<Texture2D>(@"Texts\logo");
            Background = content.Load<Texture2D>(@"Game_Backgrounds\main_menu_background");


            #region Initalize State Buttons

            Texture2D muteOn = content.Load<Texture2D>(@"Options\mute_on");
            Texture2D muteOff = content.Load<Texture2D>(@"Options\mute_off");
            muteButton = new StateButton(new Rectangle( Width - 300, Height - 150, 250, 50 ), muteOn, muteOff, OnMuteButtonClick);
            if (SoundController.isSoundMuted() )
                muteButton.IsActived = true; // Se ja tiver mudo entao o botao deve estar checado

            Texture2D fullOn = content.Load<Texture2D>(@"Options\fullscreen_on");
            Texture2D fullOff = content.Load<Texture2D>(@"Options\fullscreen_off");
            fullScreenButton = new StateButton(new Rectangle( Width - 300, Height - 100, 250, 50 ), fullOn, fullOff, OnFullscreenButtonClick);
            if( ActionsHandler.Game.graphics.IsFullScreen )
                fullScreenButton.IsActived = true; // Se ja tiver full entao o botao deve estar checado


            Texture2D tipsOn = content.Load<Texture2D>(@"Options\tips_on");
            Texture2D tipsOff = content.Load<Texture2D>(@"Options\tips_off");
            tipsButton = new StateButton(new Rectangle(Width - 300, Height - 200, 250, 50), tipsOn, tipsOff, OnTipsButtonClick);
            if( ActionsHandler.showTips )
                tipsButton.IsActived = true;


            #endregion
        }


        public override void Update(GameTime gameTime)
        {
			// o update vai ter q verificar a lista de frames...
			// se algum fram estiver ativo o canvas nao deve ser simulado
			// ou ao clicar fora do frame o frame deve ser encerrado

            tipsButton.Update();
            muteButton.Update();
            fullScreenButton.Update();
            Buttons.Update();

            SoundController.playSound("menu");
           
        }


        public override void Render(SpriteBatch renderTarget)
        {
            renderTarget.Draw(Background, new Rectangle(0, 0, Width, Height), Color.White );
            renderTarget.Draw(logo, new Rectangle(Width / 2 - logo.Width / 2, 20, logo.Width, logo.Height), Color.White);
            muteButton.Render(renderTarget);
            fullScreenButton.Render(renderTarget);
            tipsButton.Render(renderTarget);

            Buttons.Draw(renderTarget);		

        }


        public void OnMuteButtonClick(object sender, EventArgs e)
        {
            if (SoundController.isSoundMuted())
                SoundController.unMute();
            else
                SoundController.mute();

            SaveGameManager.Save();
        }

        public void OnFullscreenButtonClick(object sender, EventArgs e)
        {
            if (ActionsHandler.Game.graphics.IsFullScreen)
                ActionsHandler.Game.graphics.ToggleFullScreen();
            else
                ActionsHandler.Game.graphics.ToggleFullScreen();
            
            SaveGameManager.Save();
        }


        public void OnTipsButtonClick(object sender, EventArgs e)
        {
            ActionsHandler.showTips = ActionsHandler.showTips ? false : true;
            SaveGameManager.Save();
        }




    } // class
} // namespace
